package com.lanren.familybookapp.ui.me.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanren.common.interfaces.AdapterItemClickView;
import com.lanren.common.utils.GsonUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import imdao.cn.roundchoice.RoundChoiceView;

/**
 * @ClassName ManagerAdapter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/12 10:53
 * @Version 1.0
 */
public class ManagerAdapter extends RecyclerView.Adapter<ManagerAdapter.ContentHolder> {

    private Context mContext;
    private List<BookInfoBean> list;
    private LayoutInflater mLayoutInflater;
    private AdapterItemClickView adapterItemClickView;

    public ManagerAdapter(Context mContext, List<BookInfoBean> list) {
        this.mContext = mContext;
        this.list = list;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setAdapterItemClickView(AdapterItemClickView adapterItemClickView) {
        this.adapterItemClickView = adapterItemClickView;
    }


    @NonNull
    @Override
    public ContentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ContentHolder(mLayoutInflater.inflate(R.layout.item_book_manager, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ContentHolder contentHolder, final int i) {
        if (list.get(i).getId() == AppUserHelper.getInstance().getUser().getUserUseBook()) {
            contentHolder.bookStatus.setmChecked(true);
        } else {
            contentHolder.bookStatus.setmChecked(false);
        }

        setViewData(contentHolder, i);

        contentHolder.bookStatus.setOnCheckedChangeListener(new RoundChoiceView.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RoundChoiceView roundChoiceView, boolean b) {
                if (adapterItemClickView != null) {
                    adapterItemClickView.onItemClick(i, roundChoiceView);
                    notifyDataSetChanged();
                }
            }
        });

        contentHolder.bookItemBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterItemClickView != null) {
                    adapterItemClickView.onItemClick(i);
                }
            }
        });

        contentHolder.bookItemBg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (adapterItemClickView != null) {
                    adapterItemClickView.onItemLongClick(i);
                }
                return false;
            }
        });

    }

    private void setViewData(ContentHolder contentHolder, int pointion) {
        contentHolder.bookName.setText(list.get(pointion).getBookName());
        contentHolder.bookCreaterUser.setText(list.get(pointion).getBookCreateUser());

        String userInvite = GsonUtils.toJsonReplace(list.get(pointion).getUserInvite());

        List<Map<String, String>> mapList = GsonUtils.fromJsonToListMaps(userInvite);
        if (mapList.size() > 0) {
            String invite = "";

            for (Map<String, String> stringStringMap : mapList) {
                invite += stringStringMap.get("userInvite") + ",";
            }
            if (StringUtils.isNotBlank(invite)) {
                contentHolder.bookInvite.setText(invite.substring(0, invite.length() - 1));
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.book_name)
        TextView bookName;
        @BindView(R.id.book_status)
        RoundChoiceView bookStatus;
        @BindView(R.id.book_creater_user)
        TextView bookCreaterUser;
        @BindView(R.id.book_invite)
        TextView bookInvite;
        @BindView(R.id.book_item_bg)
        RelativeLayout bookItemBg;

        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
